package io.qar.images.service.impl;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.qar.commons.mybatis.service.impl.CrudServiceImpl;
import io.qar.images.dao.ImagesDao;
import io.qar.images.dto.ImagesDTO;
import io.qar.images.entity.ImagesEntity;
import io.qar.images.service.ImagesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * 图片列表
 *
 * @author yyyyjinying 1020066395@qq.com
 * @since 3.0 2024-07-13
 */
@Service
public class ImagesServiceImpl extends CrudServiceImpl<ImagesDao, ImagesEntity, ImagesDTO> implements ImagesService {

    @Override
    public QueryWrapper<ImagesEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<ImagesEntity> wrapper = new QueryWrapper<>();

        String filename = (String)params.get("filename");
        wrapper.like(StringUtils.isNotBlank(filename), "filename", filename);
        String imageType = (String)params.get("imageType");
        wrapper.eq(StringUtils.isNotBlank(imageType), "image_type", imageType);
        String updatedAt = (String)params.get("updatedAt");
        wrapper.eq(StringUtils.isNotBlank(updatedAt), "updated_at", updatedAt);

        return wrapper;
    }

    @Override
    public void delete(Long[] ids){
        baseDao.deleteAllByImageIds(null, ListUtil.toList(ids));
    }

    @Override
    @Transactional
    public void changeIsRecommend(List<ImagesDTO> images, List<ImagesDTO> thumbnails) {
        for (ImagesDTO image : images) {
            baseDao.changeIsRecommend(image.getIsRecommend(), image.getImageId());
        }
        for (ImagesDTO thumbnail : thumbnails) {
            baseDao.changeIsRecommend(thumbnail.getIsRecommend(), thumbnail.getImageId());
        }
    }

}